MWF.xApplication.Common = MWF.xApplication.Common || {
	options: {
		"multitask": false,	//允许多窗口运行
		"executable": true	//可执行
	}
};
MWF.require("MWF.xDesktop.Window", null, false);

MWF.xApplication.Common.Main = new Class({
	Extends: MWF.widget.Common,
	Implements: [Options, Events],
	
	options: {
		"desktopReload": true,
		"style": "default",
		"name": "Common",
		"icon": "", //图标
		"title": "", //标题
		"event": null,
		"width": "800",
		"height": "600",
		"isResize": true,
		"isMax": true,
		"setCurrent": true,
		"isRefresh": false,
        "isWindowRefresh": true
	},
	initialize: function(desktop, options){
		this.setOptions(options);
		this.desktop = desktop;

		this.path = "/x_component_"+this.options.name.replace(/\./g, "_")+"/$Main/";
		this.options.icon = this.path+this.options.style+"/"+this.options.icon;
		
		this.cssPath =this.path+this.options.style+"/css.wcss";
		this._loadCss();
	},
	fireAppEvent: function(when){
		this.fireEvent(when);
		if (this[("on-"+when).camelCase()]) this[("on-"+when).camelCase()]();
//		if (this[("on-"+when).camelCase()+"Events"){
//			this[("on-"+when).camelCase()+"Events".each(function(event){
//				event.apply(this);
//			}.bind(this));
//		}
	},
    loadNoAnimation: function(isCurrent, max, hide){
        this.fireAppEvent("queryLoad");
        if (!this.inBrowser){
            this.loadWindow(isCurrent, false, max, hide);
        }else{
            this.loadInBrowser(isCurrent);
        }
    },
	load : function(isCurrent){
		this.fireAppEvent("queryLoad");
        if (!this.inBrowser){
            this.loadWindow(isCurrent);
        }else{
            this.loadInBrowser(isCurrent);
        }
	},
	loadApplication: function(callback){
		if (callback) callback();
	},
	loadWindow: function(isCurrent, animation, max, hide){
		this.fireAppEvent("queryLoadWindow");
		var options = {
			"style": "desktop_"+this.desktop.options.style,
			"title": this.options.title,
			"isResize": this.options.isResize,
			"isMax": this.options.isMax,
            "isRefresh": this.options.isWindowRefresh,
            "container": this.desktop.node,
			"onPostShow": function(){
				if (isCurrent) this.setCurrent();
				this.fireAppEvent("postLoadWindow");
				this.fireAppEvent("queryLoadApplication");
				this.setContentEvent();
				this.loadApplication(function(){
					this.fireAppEvent("postLoadApplication");
				}.bind(this));

                var resize = false;
                var size = this.window.node.getSize();
                var position = this.window.node.getPosition();
                var bodySize = $(document.body).getSize();
                var height = size.y;
                var width = size.x;

                var y = (size.y+position.y)-bodySize.y;
                if (y>-10){
                    height = size.y-y-10;
                    resize = true;
                }
                var x = (size.x+position.x)-bodySize.x;
                if (x>-10){
                    width = size.x-x-10;
                    resize = true;
                }
                if (resize){
                    this.window.options.height = height;
                    this.window.options.width = width;
                    this.window.node.setStyles({
                        "width": ""+width+"px",
                        "height": ""+height+"px"
                    });
                    this.window.spacer.setStyles({
                        "width": ""+width+"px",
                        "height": ""+height+"px"
                    });
                    this.window.setContentSize();
                }

				this.fireAppEvent("postLoad");
			}.bind(this),
			"onResize": function(){
				this.window.options.title = this.options.title;
				this.window.getTitle();
				this.fireAppEvent("resize");
			}.bind(this),
            "onResizeCompleted": function(){
                this.fireAppEvent("resizeCompleted");
            }.bind(this),
            "onMoveDrop": function(e){
                if (Browser.name=="firefox"){
                    if (e.event.clientX<0 || e.event.clientY<0) this.openInNewBrowser();
                }else{
                    if (e.event.x<0 || e.event.y<0) this.openInNewBrowser();
                }

            }.bind(this)
			// "onQueryMax": function(){
            	// if (this.window.css.windowTitleMax) this.window.title.setStyles(this.window.css.windowTitleMax);
             //    if (this.window.css.windowTitleRefreshMax) this.window.titleRefresh.setStyles(this.window.css.windowTitleRefreshMax);
             //    if (this.window.css.windowTitleTextMax) this.window.titleText.setStyles(this.window.css.windowTitleTextMax);
             //    if (this.window.css.windowTitleActionMax) this.window.titleAction.setStyles(this.window.css.windowTitleActionMax);
			// }.bind(this)
		};
		
		if (this.options.event){
			options.fromTop = this.options.event.page.y;
			options.fromLeft = this.options.event.page.x;
		}
		options.width = this.options.width || options.width;
		options.height = this.options.height || options.height;
		
		this.window = new MWF.xDesktop.Window(this, options);
		this.fireAppEvent("loadWindow");
		//this.maxSize();
        this.content = this.window.content;
        if (animation===false){
            this.window.showNoAnimation(max, hide);
        }else{
            this.window.show();
        }
	},
    loadInBrowser: function(){
        this.window = {
            "isHide": false,
            "isMax": true,
            "maxSize": function(){},
            "restore": function(){},
            "setCurrent": function(){},
            "hide": function(){},
            "maxOrRestoreSize": function(){},
            "restoreSize": function(){},
            "close": function(){}
        };
        this.window.titleText = $(document.head).getElement("title");
        this.window.titleText.set("text", this.options.title);
        this.window.content = $("appContent");
        this.content = this.window.content;
        //this.content.setStyles({"height": "100%", "overflow": "hidden"});

        $(document.body).addEvent("resize", function(){
            this.fireAppEvent("resize");
        }.bind(this));
        window.onbeforeunload = function(e){
            this.fireAppEvent("queryClose");
        }.bind(this);

        this.fireAppEvent("postLoadWindow");
        this.fireAppEvent("queryLoadApplication");
        this.setContentEvent();
        this.loadApplication(function(){
            this.fireAppEvent("postLoadApplication");
        }.bind(this));

        this.content.setStyle("height", document.body.getSize().y);

        this.fireAppEvent("postLoad");
    },
    openInNewBrowser: function(){
        this.desktop.openBrowserApp = this.options.name;
        this.desktop.openBrowserStatus = (this.recordStatus) ? this.recordStatus() : null;
        window.open("app.html", "_blank");
        this.close();
    },
	setContentEvent: function(){
//		this.content.addEvents({
//			"resize": function(){
//				if (this.onResize) this.onResize();
//			}.bind(this)
//		});
	},
	setCurrent: function(){
		if (this.desktop.currentApp==this) return true;
		if (this.desktop.currentApp){
			this.desktop.currentApp.setUncurrent();
		}

		this.window.setCurrent();
		
		if (this.window.isHide){
			if (this.window.isMax){
				this.window.maxSize(function(){this.fireAppEvent("current");}.bind(this));
			}else{
				this.window.restore(function(){this.fireAppEvent("current");}.bind(this)); 
			}
		}else{
			this.fireAppEvent("current");
		}

		if (this.taskitem) this.taskitem.selected();
		this.desktop.currentApp = this;
		
		this.desktop.appCurrentList.erase(this);
		this.desktop.appCurrentList.push(this);
	},
	setUncurrent: function(){
		if (this.desktop.currentApp==this){
			this.window.setUncurrent();
			this.taskitem.unSelected();
			this.desktop.currentApp = null;
			this.fireAppEvent("uncurrent");
		}
	},
	minSize: function(){
		this.fireAppEvent("queryMinSize");
		var p = this.taskitem.node.getPosition();
		this.setUncurrent();
		this.window.hide(p.x, p.y, function(){
			this.fireAppEvent("postMinSize");
		}.bind(this));
	},
	_minSize: function(){

		var p = this.taskitem.node.getPosition();
		this.setUncurrent();
		this.window.hide(p.x, p.y);
	},
	maxOrRestoreSize: function(){
		var max = true;
		if (this.window.isMax) max = false;
		this.fireAppEvent((max) ? "queryMaxSize" : "queryRestoreSize");
		
		this.window.maxOrRestoreSize(function(){
			this.fireAppEvent((max) ? "postMaxSize" : "postRestoreSize");
		}.bind(this));
	},
	maxSize: function(callback){
		if (!this.window.isMax){
			this.fireAppEvent("queryMaxSize");
			this.window.maxSize(function(){
				this.fireAppEvent("postMaxSize");
				if (callback) callback();
			}.bind(this));
		}else{
            if (callback) callback();
        }
	},
	_maxSize: function(callback){
		this.window.maxSize();
	},
	restoreSize: function(callback){
		this.fireAppEvent("queryRestoreSize");
		this.window.restoreSize(function(){
			this.fireAppEvent("postRestoreSize");
			if (callback) callback();
		}.bind(this));
	},
	_restoreSize: function(callback){
		this.window.restoreSize();
	},
    refresh: function(){
        this.desktop.refreshApp(this);
    },
	close: function(){
		this.fireAppEvent("queryClose");
		this.setUncurrent();
        if (this.inBrowser){
            window.close();
        }else{
            this.window.close(function(){

                this.taskitem.destroy();
                delete this.window;
                delete this.taskitem;
                this.desktop.closeApp(this);

                this.fireAppEvent("postClose");
                //MWF.recycleCount = 0;
                //debugger;
                MWF.release(this);
                //
                //alert(MWF.recycleCount)
                //debugger;
            }.bind(this));
        }
	},
	setTitle: function(str){
		this.options.title = str;
		this.window.titleText.set("text", str);
        if (this.taskitem){
            this.taskitem.textNode.set("text", str);
            this.taskitem.node.set("title", str+((this.appId) ? "-"+this.appId : ""));
        }
	},
	
	//application事件
	onQueryLoad: function(){},
	onQueryLoadWindow: function(){},
	onLoadWindow: function(){},
	onPostLoadWindow: function(){},
	onQueryLoadApplication: function(){},
	onPostLoadApplication: function(){},
	onPostLoad: function(){},
	onResize: function(){},
	onQueryClose: function(){},
	onPostClose: function(){},
	onQueryMinSize: function(){},
	onPostMinSize: function(){},
	onQueryMaxSize: function(){},
	onPostMaxSize: function(){},
	onQueryRestoreSize: function(){},
	onPostRestoreSize: function(){},
	
	dialog: function(options){
		MWF.require("MWF.xDesktop.Dialog", function(){
			options.markNode = this.content;
			var dlg = new MWF.xDesktop.Dialog(options);
			dlg.show();
		}.bind(this));
	},
	notice: function(content, type, target, where, offset){
		if (!where) where = {"x": "right", "y": "top"};
		if (!target) target = this.window.content;
		if (!type) type = "ok";
		var noticeTarget = target || layout.layout.contentNode;
        var off = offset;
        if (!off){
            off = {
                x: 10,
                y: where.y.toString().toLowerCase()=="bottom" ? 10 : 10
            };
        }

		new mBox.Notice({
			type: type,
			position: where,
			move: false,
			target: noticeTarget,
			delayClose: (type=="error") ? 10000 : 5000,
			offset: off,
			content: content
		});
	},
	confirm: function(type, e, title, text, width, height, ok, cancel, callback, mask, style){
		MWF.require("MWF.xDesktop.Dialog", function(){
			var size = this.content.getSize();
            var x = 0;
            var y = 0;

            if (typeOf(e)=="element"){
                var position = e.getPosition(this.content);
                x = position.x;
                y = position.y;
            }else{
                if (Browser.name=="firefox"){
                    x = parseFloat(e.event.clientX);
                    y = parseFloat(e.event.clientY);
                }else{
                    x = parseFloat(e.event.x);
                    y = parseFloat(e.event.y);
                }

                if (e.target){
                    var position = e.target.getPosition(this.content);
                    x = position.x;
                    y = position.y;
                }
                //    }
            }
			
			if (x+parseFloat(width)>size.x){
				x = x-parseFloat(width);
			}
            if (x<0) x = 0;
            if (y+parseFloat(height)>size.y){
                y = y-parseFloat(height);
            }
            if (y<0) y = 0;

            var ctext = "";
            var chtml = "";
            if (typeOf(text).toLowerCase()=="object"){
                ctext = text.text;
                chtml = text.html;
            }else{
                ctext = text;
            }

            // var tmp = new Element("div", {"overflow": "hidden","padding": "10px", "padding-left": "60px", "width": ""+width+"px"}).inject(document.body);
            // if (chtml) tmp.set("html", chtml);
            // if (ctext) tmp.set("text", ctext);
            // height = tmp.getSize().y;
            // tmp.destroy();


			var dlg = new MWF.xDesktop.Dialog({
				"title": title,
				"style": style || "o2",
				"top": y,
				"left": x-20,
				"fromTop":y,
				"fromLeft": x-20,
				"width": width,
				"height": height,
				"text": ctext,
                "html": chtml,
				"container": this.content,
                "maskNode": mask,
				"buttonList": [
				    {
				    	"text": MWF.LP.process.button.ok,
				    	"action": ok
				    },
				    {
				    	"text": MWF.LP.process.button.cancel,
				    	"action": cancel
				    }
				]
			});
			
			switch (type.toLowerCase()){
				case "success":
					dlg.content.setStyle("background-image", "url()");
					break;
				case "error":
					dlg.content.setStyle("background-image", "url()");
					break;
				case "info":
					dlg.content.setStyle("background-image", "url()");
					break;
				case "warn":
					dlg.content.setStyle("background-image", "url()");
					break;
				default:
                    //dlg.content.setStyle("background-image", "");
			}
			dlg.show();
            if (callback) callback(dlg);
		}.bind(this));
	},
	alert: function(type, e, title, text, width, height){
		MWF.require("MWF.widget.Dialog", function(){
			var size = $(document.body).getSize();
			
			var x = parseFloat(e.event.x);
			var y = parseFloat(e.event.y);
            if (Browser.name=="firefox"){
                x = parseFloat(e.event.clientX);
                y = parseFloat(e.event.clientY);
            }
			x = x - parseFloat(width)/2;
			y = y - parseFloat(height)/2;
			
			if (x+parseFloat(width)>size.x){
				x = x-parseFloat(width);
			}
			var dlg = new MWF.DL({
				"title": title,
				"style": "chart",
				"top": y,
				"left": x,
				"fromTop":y,
				"fromLeft": (Browser.name=="firefox") ? e.event.clientX : e.event.x,
				"width": width,
				"height": height,
				"text": text,
				"buttonList": [
				    {
				    	"text": MWF.LP.process.button.ok,
				    	"action": function(){
				    		this.close();
				    	}
				    }
				]
			});
			
			switch (type.toLowerCase()){
				case "success":
					dlg.content.setStyle("background-image", "url()");
					break;
				case "error":
					dlg.content.setStyle("background-image", "url()");
					break;
				case "info":
					dlg.content.setStyle("background-image", "url()");
					break;
				case "warn":	
					dlg.content.setStyle("background-image", "url()");
					break;
				default: 
			}
			dlg.show();
		}.bind(this));
	}
});